using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Text;

using EntLibEC.Cmn;
using EntLibEC.Info.Basic;

namespace EntLibEC.Dac.Basic
{
	/// <summary>
	/// Summary description for ProductLastPODac.
	/// </summary>
	public class ProductLastPODac
	{
		
		public ProductLastPODac()
		{
		}

		public int Insert(ProductLastPOInfo oParam)
		{
			string sql = @"INSERT INTO Ent_Product_LastPOInfo
                            (
                            ProductSysNo, LastPOSysNo, LastQuantity, 
                            LastPrice, LastInTime, LastVendorSysNo
                            )
                            VALUES (
                            @ProductSysNo, @LastPOSysNo, @LastQuantity, 
                            @LastPrice, @LastInTime, @LastVendorSysNo
                            );set @SysNo = SCOPE_IDENTITY();";
			SqlCommand cmd = new SqlCommand(sql);

			SqlParameter paramSysNo = new SqlParameter("@SysNo", SqlDbType.Int,4);
			SqlParameter paramProductSysNo = new SqlParameter("@ProductSysNo", SqlDbType.Int,4);
			SqlParameter paramLastPOSysNo = new SqlParameter("@LastPOSysNo", SqlDbType.Int,4);
			SqlParameter paramLastQuantity = new SqlParameter("@LastQuantity", SqlDbType.Int,4);
			SqlParameter paramLastPrice = new SqlParameter("@LastPrice", SqlDbType.Decimal,9);
			SqlParameter paramLastInTime = new SqlParameter("@LastInTime", SqlDbType.DateTime);
			SqlParameter paramLastVendorSysNo = new SqlParameter("@LastVendorSysNo", SqlDbType.Int,4);

			paramSysNo.Direction = ParameterDirection.Output;

			if ( oParam.ProductSysNo != AppConst.IntNull)
				paramProductSysNo.Value = oParam.ProductSysNo;
			else
				paramProductSysNo.Value = System.DBNull.Value;
			if ( oParam.LastPOSysNo != AppConst.IntNull)
				paramLastPOSysNo.Value = oParam.LastPOSysNo;
			else
				paramLastPOSysNo.Value = System.DBNull.Value;
			if ( oParam.LastQuantity != AppConst.IntNull)
				paramLastQuantity.Value = oParam.LastQuantity;
			else
				paramLastQuantity.Value = System.DBNull.Value;
			if ( oParam.LastPrice != AppConst.DecimalNull)
				paramLastPrice.Value = oParam.LastPrice;
			else
				paramLastPrice.Value = System.DBNull.Value;
			if ( oParam.LastInTime != AppConst.DateTimeNull)
				paramLastInTime.Value = oParam.LastInTime;
			else
				paramLastInTime.Value = System.DBNull.Value;
			if ( oParam.LastVendorSysNo != AppConst.IntNull)
				paramLastVendorSysNo.Value = oParam.LastVendorSysNo;
			else
				paramLastVendorSysNo.Value = System.DBNull.Value;

			cmd.Parameters.Add(paramSysNo);
			cmd.Parameters.Add(paramProductSysNo);
			cmd.Parameters.Add(paramLastPOSysNo);
			cmd.Parameters.Add(paramLastQuantity);
			cmd.Parameters.Add(paramLastPrice);
			cmd.Parameters.Add(paramLastInTime);
			cmd.Parameters.Add(paramLastVendorSysNo);

			return SqlHelper.ExecuteNonQuery(cmd, out oParam.SysNo);
		}

		public int Update(ProductLastPOInfo oParam)
		{
			string sql = @"UPDATE Ent_Product_LastPOInfo SET 
                            ProductSysNo=@ProductSysNo, 
                            LastPOSysNo=@LastPOSysNo, LastQuantity=@LastQuantity, 
                            LastPrice=@LastPrice, LastInTime=@LastInTime, 
                            LastVendorSysNo=@LastVendorSysNo
                            WHERE SysNo=@SysNo";
			SqlCommand cmd = new SqlCommand(sql);

			SqlParameter paramSysNo = new SqlParameter("@SysNo", SqlDbType.Int,4);
			SqlParameter paramProductSysNo = new SqlParameter("@ProductSysNo", SqlDbType.Int,4);
			SqlParameter paramLastPOSysNo = new SqlParameter("@LastPOSysNo", SqlDbType.Int,4);
			SqlParameter paramLastQuantity = new SqlParameter("@LastQuantity", SqlDbType.Int,4);
			SqlParameter paramLastPrice = new SqlParameter("@LastPrice", SqlDbType.Decimal,9);
			SqlParameter paramLastInTime = new SqlParameter("@LastInTime", SqlDbType.DateTime);
			SqlParameter paramLastVendorSysNo = new SqlParameter("@LastVendorSysNo", SqlDbType.Int,4);

			if ( oParam.SysNo != AppConst.IntNull)
				paramSysNo.Value = oParam.SysNo;
			else
				paramSysNo.Value = System.DBNull.Value;
			if ( oParam.ProductSysNo != AppConst.IntNull)
				paramProductSysNo.Value = oParam.ProductSysNo;
			else
				paramProductSysNo.Value = System.DBNull.Value;
			if ( oParam.LastPOSysNo != AppConst.IntNull)
				paramLastPOSysNo.Value = oParam.LastPOSysNo;
			else
				paramLastPOSysNo.Value = System.DBNull.Value;
			if ( oParam.LastQuantity != AppConst.IntNull)
				paramLastQuantity.Value = oParam.LastQuantity;
			else
				paramLastQuantity.Value = System.DBNull.Value;
			if ( oParam.LastPrice != AppConst.DecimalNull)
				paramLastPrice.Value = oParam.LastPrice;
			else
				paramLastPrice.Value = System.DBNull.Value;
			if ( oParam.LastInTime != AppConst.DateTimeNull)
				paramLastInTime.Value = oParam.LastInTime;
			else
				paramLastInTime.Value = System.DBNull.Value;
			if ( oParam.LastVendorSysNo != AppConst.IntNull)
				paramLastVendorSysNo.Value = oParam.LastVendorSysNo;
			else
				paramLastVendorSysNo.Value = System.DBNull.Value;

			cmd.Parameters.Add(paramSysNo);
			cmd.Parameters.Add(paramProductSysNo);
			cmd.Parameters.Add(paramLastPOSysNo);
			cmd.Parameters.Add(paramLastQuantity);
			cmd.Parameters.Add(paramLastPrice);
			cmd.Parameters.Add(paramLastInTime);
			cmd.Parameters.Add(paramLastVendorSysNo);

			return SqlHelper.ExecuteNonQuery(cmd);
		}
	}
}
